package com.example.springboot.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Bookings;
import com.example.springboot.mapper.BookingsMapper;
import com.example.springboot.service.IBookingsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 民宿预订表 API控制器 - 专门用于不需要token验证的API
 * </p>
 *
 * @author
 */
@RestController
@RequestMapping("/api/bookings")
public class BookingsApi {
    @Resource
    private IBookingsService iBookingsService;
    @Resource
    private BookingsMapper bookingsMapper;



    // 新增或者更新
    @PostMapping("/add")
    public Result save(@RequestBody Bookings bookings) {
        iBookingsService.saveOrUpdate(bookings);
        return Result.success();
    }

    //分页返回
    @GetMapping("/page")
    public Result findPage(
                           @RequestParam Integer page,
                           @RequestParam Integer pageSize,
                            @RequestParam String userId) {
        QueryWrapper<Bookings> queryWrapper = new QueryWrapper<>();
        //筛选条件
        queryWrapper.eq("user_id",userId);
        queryWrapper.orderByDesc("id");

        return Result.success(iBookingsService.page(new Page<>(page, pageSize), queryWrapper));
    }

    //查看密码
    @GetMapping("/viewPassword/{id}")
    public  Result view(@PathVariable Integer id){

        // 调用服务层方法更新审批状态，并存储密码
        String password=  bookingsMapper.selectPawweord(id);

        // 返回成功结果
        return Result.success(password);
    }

    //更新支付状态
    @PostMapping("/view/{id}")
    public Result updateStatus(@PathVariable Integer id) {
        bookingsMapper.updateStatus(id);

        return Result.success();
    }

    //更新状态
    @PostMapping("/deleteView/{id}")
    public Result deleteView(@PathVariable Integer id) {
        bookingsMapper.deleteView(id);

        return Result.success();
    }

    //更新状态
    @PostMapping("/refund/{id}")
    public Result Refund(@PathVariable Integer id) {
        bookingsMapper.Refund(id);

        return Result.success();
    }
    //删除预订
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        iBookingsService.removeById(id);
        return Result.success();
    }

}
